using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using WorkflowProcesses = Scenario.WorkflowEngine.DataModel.WorkflowProcesses;
using WorkflowRelevantData = Scenario.WorkflowEngine.DataModel.WorkflowRelevantData;
using WorkflowParticipantsData = Scenario.WorkflowEngine.DataModel.WorkflowParticipantsData;

using WAPI = Scenario.WAPI;
using Scenario.WorkflowEngine;

namespace Scenario.Forms.Workflow
{
	/// <summary>
	/// Descrizione di riepilogo per ProcessesList.
	/// </summary>
	public class ProcessesList : System.Windows.Forms.Form
    {
        private IContainer components;

		public WorkflowProcesses.XMLWorkflows xmlWorkflows;
		private System.Windows.Forms.ListView lwProcesses;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.RadioButton OptProcessList;
		private System.Windows.Forms.RadioButton optOneProcess;
		private System.Windows.Forms.Button btnLoad;
        private System.Windows.Forms.TextBox txtProcessName;
        private Button button2;
        private ContextMenuStrip contextMenuStrip1;
        private ToolStripMenuItem mnuDetails;
        private ToolStripSeparator pippoToolStripMenuItem;
        private ToolStripMenuItem mnuRestart;
		public WorkflowProcesses.XMLWorkflow XMLWorkflow;

		public ProcessesList()
		{
			//
			// Necessario per il supporto di Progettazione Windows Form
			//
			InitializeComponent();

			this.MakeGrid();
		}

		private void MakeGrid()
		{
			ColumnHeader ch = new ColumnHeader();
			ch.Text = "ProcessId";
			ch.TextAlign = HorizontalAlignment.Right;
			ch.Width = 100;
			lwProcesses.Columns.Add(ch);

			ch = new ColumnHeader();
			ch.Text = "Name";
			ch.TextAlign = HorizontalAlignment.Left;
			ch.Width = 100;
			lwProcesses.Columns.Add(ch);

			ch = new ColumnHeader();
			ch.Text = "Description";
			ch.TextAlign = HorizontalAlignment.Left;
			ch.Width = 100;
			lwProcesses.Columns.Add(ch);

			ch = new ColumnHeader();
			ch.Text = "Status";
			ch.TextAlign = HorizontalAlignment.Left;
			ch.Width = 100;
			lwProcesses.Columns.Add(ch);
		}

		public WorkflowProcesses.XMLWorkflows XMLWorkflows 
		{
			get { return xmlWorkflows;}
			set 
			{
				xmlWorkflows = value;

				WorkflowProcesses.XMLWorkflow workflow = null;
				for ( int i=0; i<xmlWorkflows.Count;i++)
				{
					workflow = (WorkflowProcesses.XMLWorkflow)xmlWorkflows[i];
					ListViewItem item = new ListViewItem();
					item.Text = workflow.ProcessId.ToString();
					item.SubItems.Add(workflow.Name);
					item.SubItems.Add(workflow.Description);
					item.SubItems.Add(workflow.Status);
					this.lwProcesses.Items.Add(item);
				}
			}
		}
			

		/// <summary>
		/// Pulire le risorse in uso.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Codice generato da Progettazione Windows Form
		/// <summary>
		/// Metodo necessario per il supporto della finestra di progettazione. Non modificare
		/// il contenuto del metodo con l'editor di codice.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            this.lwProcesses = new System.Windows.Forms.ListView();
            this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.mnuDetails = new System.Windows.Forms.ToolStripMenuItem();
            this.pippoToolStripMenuItem = new System.Windows.Forms.ToolStripSeparator();
            this.mnuRestart = new System.Windows.Forms.ToolStripMenuItem();
            this.panel1 = new System.Windows.Forms.Panel();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.button2 = new System.Windows.Forms.Button();
            this.btnLoad = new System.Windows.Forms.Button();
            this.txtProcessName = new System.Windows.Forms.TextBox();
            this.optOneProcess = new System.Windows.Forms.RadioButton();
            this.OptProcessList = new System.Windows.Forms.RadioButton();
            this.contextMenuStrip1.SuspendLayout();
            this.panel1.SuspendLayout();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // lwProcesses
            // 
            this.lwProcesses.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.lwProcesses.ContextMenuStrip = this.contextMenuStrip1;
            this.lwProcesses.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.lwProcesses.FullRowSelect = true;
            this.lwProcesses.GridLines = true;
            this.lwProcesses.Location = new System.Drawing.Point(0, 109);
            this.lwProcesses.Name = "lwProcesses";
            this.lwProcesses.Size = new System.Drawing.Size(712, 651);
            this.lwProcesses.TabIndex = 0;
            this.lwProcesses.UseCompatibleStateImageBehavior = false;
            this.lwProcesses.View = System.Windows.Forms.View.Details;
            this.lwProcesses.DoubleClick += new System.EventHandler(this.lwProcesses_DoubleClick);
            // 
            // contextMenuStrip1
            // 
            this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.mnuDetails,
            this.pippoToolStripMenuItem,
            this.mnuRestart});
            this.contextMenuStrip1.Name = "contextMenuStrip1";
            this.contextMenuStrip1.Size = new System.Drawing.Size(154, 54);
            this.contextMenuStrip1.Opening += new System.ComponentModel.CancelEventHandler(this.contextMenuStrip1_Opening);
            // 
            // mnuDetails
            // 
            this.mnuDetails.Name = "mnuDetails";
            this.mnuDetails.Size = new System.Drawing.Size(153, 22);
            this.mnuDetails.Text = "Process Details";
            this.mnuDetails.Click += new System.EventHandler(this.mnuDetails_Click);
            // 
            // pippoToolStripMenuItem
            // 
            this.pippoToolStripMenuItem.Name = "pippoToolStripMenuItem";
            this.pippoToolStripMenuItem.Size = new System.Drawing.Size(150, 6);
            // 
            // mnuRestart
            // 
            this.mnuRestart.Name = "mnuRestart";
            this.mnuRestart.Size = new System.Drawing.Size(153, 22);
            this.mnuRestart.Text = "Restart Process";
            this.mnuRestart.Click += new System.EventHandler(this.mnuRestart_Click);
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.groupBox1);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(712, 127);
            this.panel1.TabIndex = 1;
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.button2);
            this.groupBox1.Controls.Add(this.btnLoad);
            this.groupBox1.Controls.Add(this.txtProcessName);
            this.groupBox1.Controls.Add(this.optOneProcess);
            this.groupBox1.Controls.Add(this.OptProcessList);
            this.groupBox1.Location = new System.Drawing.Point(7, 14);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(693, 97);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Load";
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(603, 53);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 35);
            this.button2.TabIndex = 1;
            this.button2.Text = "Cancel";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // btnLoad
            // 
            this.btnLoad.Location = new System.Drawing.Point(458, 53);
            this.btnLoad.Name = "btnLoad";
            this.btnLoad.Size = new System.Drawing.Size(139, 35);
            this.btnLoad.TabIndex = 3;
            this.btnLoad.Text = "Load Process Instances";
            this.btnLoad.Click += new System.EventHandler(this.btnLoad_Click);
            // 
            // txtProcessName
            // 
            this.txtProcessName.Enabled = false;
            this.txtProcessName.Location = new System.Drawing.Point(13, 68);
            this.txtProcessName.Name = "txtProcessName";
            this.txtProcessName.Size = new System.Drawing.Size(439, 20);
            this.txtProcessName.TabIndex = 2;
            this.txtProcessName.Text = "<ProcessName>";
            // 
            // optOneProcess
            // 
            this.optOneProcess.Location = new System.Drawing.Point(13, 41);
            this.optOneProcess.Name = "optOneProcess";
            this.optOneProcess.Size = new System.Drawing.Size(87, 21);
            this.optOneProcess.TabIndex = 1;
            this.optOneProcess.Text = "Process";
            this.optOneProcess.CheckedChanged += new System.EventHandler(this.optOneProcess_CheckedChanged);
            // 
            // OptProcessList
            // 
            this.OptProcessList.Checked = true;
            this.OptProcessList.Location = new System.Drawing.Point(13, 19);
            this.OptProcessList.Name = "OptProcessList";
            this.OptProcessList.Size = new System.Drawing.Size(100, 28);
            this.OptProcessList.TabIndex = 0;
            this.OptProcessList.TabStop = true;
            this.OptProcessList.Text = "All Processes";
            // 
            // ProcessesList
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(712, 760);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.lwProcesses);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "ProcessesList";
            this.Text = "ProcessesList";
            this.contextMenuStrip1.ResumeLayout(false);
            this.panel1.ResumeLayout(false);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.ResumeLayout(false);

		}
		#endregion

		private void optOneProcess_CheckedChanged(object sender, System.EventArgs e)
		{
			if ( optOneProcess.Checked == true )
			{
				this.txtProcessName.Enabled  = true;
			}
			else
			{
				this.txtProcessName.Enabled = false;
			}
		}

        private void LoadProcess(string processId)
		{
            XMLWorkflow = Engine.ProcessHelper.GetWorkflowInstance(processId);
		}

		private void btnLoad_Click(object sender, System.EventArgs e)
		{
			if ( optOneProcess.Checked == true )
			{
                if ( this.txtProcessName.Text != string.Empty ) 
				{
					MessageBox.Show ("TODO");
					
				}
				else
				{
					MessageBox.Show ("It's mandatory select process Id or name");
					
				}
			}
			else
			{
                this.XMLWorkflows = Engine.ProcessHelper.GetWorkflowInstances(); 
			}
		}

		private void lwProcesses_DoubleClick(object sender, System.EventArgs e)
		{
			this.LoadProcess (this.lwProcesses.SelectedItems[0].SubItems[0].Text);
			this.Close();
		}

        private void button2_Click(object sender, EventArgs e)
        {
            this.XMLWorkflow = null;
            this.Close();
        }

        private void mnuRestart_Click(object sender, EventArgs e)
        {
            if (this.lwProcesses.SelectedItems.Count == 0)
            {
                MessageBox.Show("Select a process");
                return;
            }
            else if (this.lwProcesses.SelectedItems.Count > 1)
            {
                MessageBox.Show("Select only one process");
                return;
            }

            ;
            this.LoadProcess(this.lwProcesses.SelectedItems[0].SubItems[0].Text);

            //this.Close();

            if (this.XMLWorkflow != null)
            {
                Scenario.Forms.Workflow.ProcessInstanceManager instanceManager =
                    new Scenario.Forms.Workflow.ProcessInstanceManager();
                instanceManager.LoadProcess(ref this.XMLWorkflow);
                instanceManager.ShowDialog();
            }
        }

        private void mnuDetails_Click(object sender, EventArgs e)
        {
            MessageBox.Show("TODO");
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {

        }
	}
}
